package com.cos66;

import java.util.ArrayList;
import java.util.List;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.cos66.intf.MusicState;
import com.cos66.model.MagazineDetailItem;
import com.cos66.utils.Cos66Setting;
import com.cos66.utils.FileUtils;

public class MusicService extends Service {

	private static final String TAG = "MusicService";

	public static final int DO_NOTHING = 0;
	public static final int PLAY_MUSIC = 1;
	public static final int PLAY_PAUSE_MUSIC = 2;
	public static final int PLAY_MUSIC_WITHOUT_PAUSE = 3;
	public static final int SEEK_MUSIC = 4;
	public static final int PLAY_NEXT_MUSIC = 5;
	public static final int PLAY_BACK_MUSIC = 6;
	public static final int STOP_MUSIC = 7;
	
	public static final String OPERATE_COMMAND = "OC";
	public static final String DATA_RESOURCE = "DR";
	public static final String SEEK_POSITION = "SP";
	public static final String DATA_LIST = "DL";

	private MediaPlayer mediaPlayer;

	private MusicInfo musicInfo = new MusicInfo();

	ArrayList<MagazineDetailItem> musicData;

	List<String> musicResource = new ArrayList<String>();
	List<String> musicName = new ArrayList<String>();

	private String nowmusicsource = "";

	private int index = 0;

	public class MusicInfo extends Binder implements MusicState {

		public int getPosition() {
			try {
				return mediaPlayer.getCurrentPosition();
			} catch (Exception e) {
				return 0;
			}
		}

		public int getDuration() {
			try {
				return mediaPlayer.getDuration();
			} catch (Exception e) {
				return 0;
			}
		}

		public boolean isPlaying() {
			return mediaPlayer.isPlaying();
		}

		public String getSongName() {
			try {
				return musicName.get(index);
			} catch (Exception e) {
				return "Unknow";
			}
		}

	}

	@Override
	public IBinder onBind(Intent intent) {
		return musicInfo;
	}

	@Override
	public void onCreate() {
		Log.v(TAG, "onCreate");
		if (mediaPlayer == null) {
			mediaPlayer = new MediaPlayer();
			mediaPlayer.setLooping(false);
			mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				
				public void onCompletion(MediaPlayer mp) {
					playnext();
				}
			});
		}
	}

	@Override
	public void onDestroy() {
		Log.v(TAG, "onDestroy");
		if (mediaPlayer != null) {
			mediaPlayer.stop();
			mediaPlayer.release();
		}
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if (intent != null) {
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				musicData = bundle.getParcelableArrayList(DATA_LIST);
				if (musicData != null) {
					for (MagazineDetailItem item : musicData) {
						musicResource.add(item.getDetailResourceUrl());
						musicName.add(item.getDetailTitle());
					}
				}
				int op = bundle.getInt(OPERATE_COMMAND);
				String willmusicsource = bundle.getString(DATA_RESOURCE);
				switch (op) {
				case PLAY_MUSIC:
					play(willmusicsource);
					break;
				case PLAY_PAUSE_MUSIC:
					playorpasue();
					break;
				case PLAY_MUSIC_WITHOUT_PAUSE:
					playother(willmusicsource);
					break;
				case SEEK_MUSIC:
					int position = bundle.getInt(SEEK_POSITION);
					seek(position);
					break;
				case PLAY_NEXT_MUSIC:
					playnext();
					break;
				case PLAY_BACK_MUSIC:
					playback();
					break;
				case STOP_MUSIC:
					stop();
					break;
				default:
					break;
				}
			}
		}
		return super.onStartCommand(intent, flags, startId);
	}

	public void seek(int position) {
		if (mediaPlayer != null) {
			mediaPlayer.seekTo(position);
		}
	}

	public void playorpasue(){
		if (!mediaPlayer.isPlaying()) {
			mediaPlayer.start();
		} else {
			mediaPlayer.pause();
		}
	}
	
	public void play(String willmusicsource) {
		prepareplaynext(willmusicsource);
		playorpasue();
	}

	public void playother(String willmusicsource) {
		prepareplaynext(willmusicsource);
		if (!mediaPlayer.isPlaying()) {
			mediaPlayer.start();
		}
	}

	public void playnext() {
		index = musicResource.indexOf(nowmusicsource) + 1;
		if (index >= musicResource.size()) {
			index = 0;
		}
		String willmusicsource = musicResource.get(index);
		playother(willmusicsource);
	}

	public void playback() {
		index = musicResource.indexOf(nowmusicsource) - 1;
		if (index < 0) {
			index = musicResource.size() - 1;
		}
		String willmusicsource = musicResource.get(index);
		playother(willmusicsource);
	}

	public void stop() {
		if (mediaPlayer != null) {
			mediaPlayer.stop();
		}
	}

	public void prepareplaynext(String willmusicsource) {
		if (!willmusicsource.equals(nowmusicsource)) {
			try {
				if(!"".equals(nowmusicsource)){
					mediaPlayer.reset();
				}
				mediaPlayer.setDataSource("file:///" + FileUtils.getSDPATH()
						+ Cos66Setting.NATIVE_PATH + willmusicsource);
				mediaPlayer.prepare();
				nowmusicsource = willmusicsource;
				index = musicResource.indexOf(nowmusicsource);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
